ANMMT_PWD='xxx'
MYSQL_PWD='xxx'

/************************************************************************/
# step1：安装percona tool kits

yum install perl-IO-Socket-SSL perl-DBD-MySQL perl-Time-HiRes -y
rpm -ivh percona-toolkit...

/************************************************************************/
# step2：安装box annemometer

下载：https://github.com/box/Anemometer

unzip Anemometer-master
yum install  httpd php *bcmath* *mysqli*  -y  # 注php版本应该大于5.3，否则从epel源安装相应php版本
mv Anemometer-master /var/www/html/anemometer
mysql -uroot -p${MYSQL_PWD} <./install.sql


mysql -uroot -p${MYSQL_PWD} <<EOF
create user anemometer@'%' identified by '${ANMMT_PWD}';
create user anemometer@'localhost' identified by '${ANMMT_PWD}';

grant all on slow_query_log.* to anemometer@'%';
grant all on slow_query_log.* to anemometer@'localhost';
grant select on *.* to anemometer@'%';
grant select on *.* to anemometer@'localhost';
EOF

/************************************************************************/
# step3：配置php

# 修改php扩展插件

grep -A10 MySQLi /etc/php.ini 
[MySQLi]

; Maximum number of links.  -1 means no limit.
; http://www.php.net/manual/en/mysqli.configuration.php#ini.mysqli.max-links
mysqli.max_links = -1
extension = php_mysqli.dll
extension = mysqli.so


#修改时区为Asia/Shanghai

grep date.timezone /etc/php.ini 
; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone
date.timezone = Asia/Shanghai


# 修改时区Asia/Shanghai与PRC

grep -A20 'construct' /var/www/html/anemometer/lib/Anemometer.php
    function __construct($conf)
    {
        $this->load = new Loader();
        if (empty($conf))
        {
            return;
        }

        $timezone = ini_get('date.timezone','Asia/Shanghai');
        if (!$timezone)
        {
            $system_timezone = exec('date +%Z');
            date_default_timezone_set($system_timezone);
            $timezone = date_default_timezone_get('PRC');


/************************************************************************/
# step4：配置anemometer

cp conf/sample.config.inc.php conf/config.inc.php


#添加数据源（可多个）

cat conf/config.inc.php
$conf['datasources']['freemeos_slave'] = array(
        'host'  => 'localhost',
        'port'  => 3306,
        'db'    => 'slow_query_log',
        'user'  => 'xxx',
        'password' => 'xxx',
        'tables' => array(
                'global_query_review' => 'fact',
                'global_query_review_history' => 'dimension'
        ),
        'source_type' => 'slow_query_log'
);


#修改visual_explain指向正确路径
#修改explain时使用的用户名密码
$conf['plugins'] = array(

        'visual_explain' => '/usr/local/bin/pt-visual-explain',
#       percona toolkit has removed query advisor
#       'query_advisor' => '/usr/bin/pt-query-advisor',

        'show_create'   => true,
        'show_status'   => true,

        'explain'       =>      function ($sample) {
                $conn = array();

                if (!array_key_exists('hostname_max',$sample) or strlen($sample['hostname_max']) < 5)
                {
                        return;
                }

                $pos = strpos($sample['hostname_max'], ':');
                if ($pos === false)
                {
                        $conn['port'] = 3306;
                        $conn['host'] = $sample['hostname_max'];
                }
                else
                {
                        $parts = preg_split("/:/", $sample['hostname_max']);
                        $conn['host'] = $parts[0];
                        $conn['port'] = $parts[1];
                }

                $conn['db'] = 'mysql';
                if ($sample['db_max'] != '')
                {
                        $conn['db'] = $sample['db_max'];
                }

                $conn['user'] = 'xxx';
                $conn['password'] = 'xxx';


/************************************************************************/
# step4：开启anemometer

service httpd start;chkconfig httpd on
http://ipaddr/anemometer

/************************************************************************/
# step5：修改hosts

如果box anemometer显示执行计划时报如下错误：

error: Unknown MySQL server host 'n01' (1)(2005)

则将对应目标服务器的ip hostname添加到本地/etc/hosts中。